<template>
    <div class="users">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/menu/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/menu/">用户管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <!-- 搜索 -->
            <div class="search">
                <el-input v-model="kw" placeholder="请输入内容" clearable @clear="clearsearch"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
                <!-- 添加 -->
                <el-button type="info" @click="dialogFormVisible = true">添加</el-button>
            </div>
            <!-- 表格 -->
            <el-table :data="tableData" border style="width: 100%">
                <el-table-column type="index" prop="index" label="#">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="手机">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column label="状态" prop="mg_state">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" active-color="#02538C" inactive-color="#B9B9B9"
                            @change="changeSwitch(scope.row)" />
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" @click="update(scope.row)" size="mini" circle>
                        </el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="del(scope.row)" size="mini" circle>
                        </el-button>
                        <el-button type="warning" icon="el-icon-setting" @click="roles(scope.row)" size="mini" circle>
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[3,5,7,9]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="this.total">
            </el-pagination>
        </el-card>
        <!-- 添加的模态框 -->
        <el-dialog title="添加" :visible.sync="dialogFormVisible">
            <el-form :rules="rules" ref="form" :model="form" label-width="80px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="tel">
                    <el-input v-model="form.mobile" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="add()">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 编辑的模态框 -->
        <el-dialog title="编辑" :visible.sync="dialogFormVisible1">
            <el-form :rules="rules" ref="form" :model="form" label-width="80px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" disabled autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="form.mobile" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="do_update()">修 改</el-button>
            </div>
        </el-dialog>
        <!-- 分配角色的模态框 -->
        <el-dialog title="分配角色" :visible.sync="dialogFormVisible2">
            <el-form :rules="rules" ref="form" :model="form" label-width="80px">
                <p>当前用户 : {{user}}</p>
                <p>当前角色 : {{role}}</p>
                <el-form-item label="分配角色">
                    <el-select v-model="form.region" placeholder="请选择分配的角色">
                        <el-option v-for="item,index in rolelist" :key="index" :label="item.roleName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="do_role()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            kw: '',
            dialogFormVisible: false,
            dialogFormVisible1: false,
            dialogFormVisible2: false,
            tableData: [],
            rolelist: [],
            user: '',
            role: '',
            form: {
                username: '',
                password: '',
                email: '',
                mobile: '',
                region:'',
            },
            rules: {
                username: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },
                ],
                email: [
                    { required: true, message: '邮箱不能为空', trigger: 'blur' },
                ],
                mobile: [
                    { required: true, message: '手机不能为空', trigger: 'blur' },
                ],
            },
            queryInfo: {
                query: '',
                pagenum: 1,
                pagesize: 5
            },
            total: 0,

            id: 0,
            uid:0,
        }
    },
    mounted() {
        this.getlist()
    },
    methods: {
        // 请求数据
        async getlist() {
            var res = await this.axios({
                url: '/v1/users',
                params: this.queryInfo
            })
            console.log(res);
            this.tableData = res.data.data.users
            this.total = res.data.data.total
        },
        // 分页
        handleSizeChange(val) {
            this.queryInfo.pagesize = val
            console.log(this.queryInfo.pagesize);
            this.getlist()
        },
        handleCurrentChange(val) {
            this.queryInfo.pagenum = val
            console.log(this.queryInfo.pagenum);
            this.getlist()
        },
        // // 修改数据状态
        async changeSwitch(row) {
            var { id } = row
            console.log(id);
            var { mg_state } = row
            var res = await this.axios({
                url: `/v1/users/${id}/state/${mg_state}`,
                method: "PUT"
            })
            if (res.data.meta.status == 200) {
                this.$message.success("状态设置成功")
            }
        },
        // 删除
        del(row) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                var id = row.id
                var res = await this.axios({
                    url: `/v1/users/${id}`,
                    method: 'DELETE',
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getlist()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 添加
        add() {
            this.$refs.form.validate(async (valid) => {
                if (!valid) {
                    return this.$message.error("校验失败")
                } else {
                    var res = await this.axios({
                        url: '/v1/users',
                        method: "POST",
                        data: this.form
                    })
                    this.dialogFormVisible = false
                    console.log(res);
                    if (res.data.meta.status == 201) {
                        this.$message.success('创建成功')
                        this.getlist()
                        this.form = {}
                    }
                }
            })
        },
        // 搜索
        async search() {
            var res = await this.axios({
                url: '/v1/users',
                params: {
                    pagesize: this.queryInfo.pagesize,
                    pagenum: this.queryInfo.pagenum,
                    query: this.kw
                }
            })
            console.log(res);
            this.tableData = res.data.data.users
            this.total = res.data.data.total
        },
        // 清空搜索框
        clearsearch() {
            this.queryInfo.query = ''
            this.getlist()
        },
        // 编辑
        update(row) {
            this.id = row.id
            this.dialogFormVisible1 = true,
                this.form = row

        },
        do_update() {
            var id = this.id
            this.$refs.form.validate(async (valid) => {
                if (!valid) {
                    return this.$message.error("校验失败")
                } else {
                    var res = await this.axios({
                        url: `/v1/users/${id}`,
                        method: "PUT",
                        data: {
                            username: this.form.username,
                            email: this.form.email,
                            mobile: this.form.mobile,
                        }
                    })
                    console.log(res);
                    if (res.data.meta.status == 200) {
                        this.dialogFormVisible1 = false
                        this.$message.success('编辑成功')
                        this.getlist()
                    }
                }
            })
        },
        //分配角色
        async roles(row) {
            var res = await this.axios({
                url: '/v1/roles',
            })
            this.rolelist=res.data.data
            console.log(this.rolelist);
            this.user = row.username,
            this.role = row.role_name,
            this.uid =row.id
            this.dialogFormVisible2 = true
        },
       async do_role() {
        let uid = this.uid
        let rid = this.form.region
        console.log(uid,"uid");
        console.log(rid,"rid");
         var res = await this.axios({
            url:`/v1/users/${uid}/role`,
            method:'PUT',
            data:{
                rid:rid
            }
         })
         console.log(res);
         if(res.data.meta.status==200){
            this.$message.success('角色设置成功')
            this.dialogFormVisible2 = false
         }
        }
        // 作弊刷添加
        //  tj(){
        //     setInterval(async ()=>{
        //         var res = await this.axios({
        //                 url: '/v1/users',
        //                 method: "POST",
        //                 data: {
        //                     username: nanoid,
        //                     password:'123',
        //                     email:'123456@qq.com',
        //                     mobile:'110'
        //                 }
        //             })
        //             this.dialogFormVisible = false
        //             console.log(res);
        //             if (res.data.meta.status == 201) {
        //                 this.getlist()

        //             }
        //     },10)
        //  }
    }
}
</script>
<style >
.box-card {
    width: 100%;
    margin-top: .9375rem;
}

.search {
    display: flex;

}

.search .el-input__inner {
    width: 300px;
}

.search .el-input {
    width: 300px;
}
</style>